package com.fili.editor;

import java.awt.*;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Fili
 * Date: Mar 11, 2007
 * Time: 1:35:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class BezierPath
{
    public static final double SELECTION_RADIUS = 4;
    private ArrayList<BezierCurve> curves;


    public BezierPath()
    {
        curves = new ArrayList<BezierCurve>();
        BezierCurve bc = new BezierCurve();
        bc.setPoints(new Point[]{new Point(100, 100), new Point(100, 50), new Point(200, 50), new Point(200, 100)});
        curves.add(bc);
    }

    public void render(Graphics2D g)
    {
        for (BezierCurve curve : curves)
            curve.render(g);
    }

    public double select(int x, int y)
    {
        double min = 100000;
        BezierCurve c;

        c = curves.get(0);
        min = c.dist(x, y);


        for (int i = 1; i < curves.size(); i++)
        {
            BezierCurve bc = curves.get(i);
            double m = bc.dist(x, y);
            if (m < min)
            {
                min = m;
                c = bc;
            }
        }
        deselect();

        if (min < SELECTION_RADIUS)
        {
            c.select(x,y);
        }

        return 1000;
    }

    public void deselect()
    {
        for (BezierCurve bc : curves)
            bc.setSelected(-1);
    }

    public void drag(int x, int y)
    {
        for (BezierCurve bc : curves)
            bc.drag(x,y);
    }
}
